<?php

/**
 * @author agrouzdev
 * @package faq
 * @version 1.0
 */

function smarty_function_faq($params, &$smarty)
{
	$smarty->Controller->LoadModule('faq');
	$action = strtoupper(	PrepareVar_1($params['action']) );
	//var_dump($smarty->PageController->module);
	if (!defined('FWAD_FAQ_ACTION_'.$action))
	{
		if (is_a($smarty->Controller,"AdminController"))
			echo "Неправильно задан формат action. Такого действия для заданной функции не существует: ".$action;
		return;
	}
	
	$act = constant('FWAD_FAQ_ACTION_'.$action);
	switch ($act)
	{
		case FWAD_FAQ_ACTION_GETALLITEMS:
			$start = isset($params['start']) && is_numeric($params['start']) ? $params['start'] : 0;
			$count = isset($params['count']) && is_numeric($params['count']) ? $params['count'] : -1;
			$publish = isset($params['publish']) ? strtolower($params['publish']) : 'all';
			if (($publish!='y')&&($publish!='n')&&($publish!='all')) $publish = 'all';
			//$res = $smarty->Controller->Modules['faq']->faq->GetItemsList($publish,$start ,$count);
			$smarty->assign($params['varname'],$smarty->Controller->Modules['faq']->faq->GetItemsList($publish,$start ,$count));
			break;
		case FWAD_FAQ_ACTION_GETITEMSCNT:
			$publish = isset($params['publish']) ? strtolower($params['publish']) : 'all';
			if (($publish!='y')&&($publish!='n')&&($publish!='all')) $publish = 'all';
			$smarty->assign($params['varname'],	$smarty->Controller->Modules['faq']->faq->GetItemsCount($publish));
			break;
		case FWAD_FAQ_ACTION_GETITEM:
			if ( isset($params['id']) && is_numeric($params['id']))
				$smarty->assign($params['varname'],	$smarty->Controller->Modules['faq']->faq->GetItem($params['id']));
			else echo "Неправильно задан формат id";
			break;
		case FWAD_FAQ_ACTION_USERASK:
			$params['q_email'] = PrepareVar_1($_POST['email']);
			if ((!eregi('^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$',$params['q_email'])))
				return;// "Вы ввели адрес электронной почты в неправильном формате";
			$params1['q_email'] = PrepareVar_1($params['q_email']);
			$params1['q_author'] = PrepareVar_1($_POST['name']);		
			$params1['q_koord'] = PrepareVar_1($_POST['info']);
			$params1['q_text'] = PrepareVar_1($_POST['question']);
			$params1['q_date'] = date ("Y-m-d H:i:s");
			$params1['published'] = 'n';
			$smarty->Controller->Modules['faq']->faq->AddItem($params1);
			break;			
	}
}
?>